import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '/utils/G.dart';

class Splash extends StatefulWidget {
  const Splash({Key? key}) : super(key: key);

  @override
  _SplashState createState() => _SplashState();
}

class _SplashState extends State<Splash> {
  int counter = 3;
  late Timer _timer;

  @override
  void initState() {
    super.initState();

    // 隐藏状态栏
    SystemChrome.setEnabledSystemUIOverlays([]);

    countDown();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: const Alignment(1.0, -1.0),
      children: [
        ConstrainedBox(
            constraints: const BoxConstraints.expand(),
            child: Image.asset(
              "assets/images/splash.jpg",
              fit: BoxFit.fill,
            )),
        Container(
          color: Colors.grey,
          margin: const EdgeInsets.fromLTRB(0, 30, 20, 0),
          padding: const EdgeInsets.fromLTRB(5, 1, 5, 1),
          child: TextButton(
              onPressed: () {
                goHome();
              },
              child: Text("$counter 跳过广告",
                  style: const TextStyle(color: Colors.white, fontSize: 14))),
        )
      ],
    );
  }

  countDown() {
    var _duration = const Duration(seconds: 1);

    Timer(_duration, () {
      _timer = Timer.periodic(_duration, (timer) {
        counter--;
        if (counter == 0) {
          goHome();
        } else {
          setState(() {});
        }
      });
    });
  }

  void goHome() {
    _timer.cancel();
    G.router.navigateTo(context, "/");
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
}
